package com.bingkaimedia.merchantbeta.app;
 

import java.util.HashMap;

import com.bingkaimedia.merchantbeta.R;
import com.bingkaimedia.merchantbeta.libs.SessionManager;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class MenuKiri extends ListFragment {


 	SessionManager session;

	private static String session_userid;
	private static String session_username;
	private static String session_password;
	private static String session_email;
	private static String session_country;
	private static String session_country_name;
	private static String session_city;
	private static String session_city_name;
	private static String session_checked;
	private static String session_channel;
	private static String session_category;
	private static String session_category_name;

	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		
		session = new SessionManager(getActivity().getApplicationContext());
	    
	    HashMap<String, String> user = session.getUserDetails();
	     
	    // name
	    session_userid = user.get(SessionManager.id_users);
	    session_username = user.get(SessionManager.username);
	    session_password = user.get(SessionManager.password);
	    session_email = user.get(SessionManager.email);
	    session_country = user.get(SessionManager.country);
	    session_country_name = user.get(SessionManager.country_name);
	    session_city = user.get(SessionManager.city);
	    session_city_name = user.get(SessionManager.city_name);
	    session_checked = user.get(SessionManager.checked);
	    session_channel = user.get(SessionManager.channel);
	    session_category = "";
	    session_category_name = "";
	    
	    
		SampleAdapter adapter = new SampleAdapter(getActivity());
		String[] separated = session_username.split("@");
		adapter.add(new SampleItem("Home", R.drawable.diskonion_icon_home));
		adapter.add(new SampleItem("Profile [ "+ separated[0] + " ]", R.drawable.diskonion_icon_profile));
		adapter.add(new SampleItem("Countries [ "+session_country_name + " ]", R.drawable.diskonion_icon_country));
		adapter.add(new SampleItem("City [ "+session_city_name + " ]", R.drawable.diskonion_icon_city));
		adapter.add(new SampleItem("My Voucher", R.drawable.diskonion_icon_myvocer));
		adapter.add(new SampleItem("My Channel", R.drawable.diskonion_icon_mycenel));
		adapter.add(new SampleItem("Other Channels", R.drawable.diskonion_icon_mycenellain));
		adapter.add(new SampleItem("Share", R.drawable.diskonion_icon_myshare));
		adapter.add(new SampleItem("Help", R.drawable.diskonion_icon_help));
		adapter.add(new SampleItem("About", R.drawable.diskonion_polos));
		adapter.add(new SampleItem("Sign Out", R.drawable.diskonion_icon_exit));
		 
		setListAdapter(adapter);
	}

	private class SampleItem {
		public String tag;
		public int iconRes;
		public SampleItem(String tag, int iconRes) {
			this.tag = tag; 
			this.iconRes = iconRes;
		}
	}

	public class SampleAdapter extends ArrayAdapter<SampleItem> {

		public SampleAdapter(Context context) {
			super(context, 0);
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_list_menu, null);
			}
			ImageView icon = (ImageView) convertView.findViewById(R.id.row_icon);
			icon.setImageResource(getItem(position).iconRes);
			TextView title = (TextView) convertView.findViewById(R.id.row_title);
			title.setText(getItem(position).tag);

			return convertView;
		}

	}
	
	
	
	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		
		
		Intent extras = getActivity().getIntent();
		 
		
		Intent intent = null;
		//((MenuLeft)getActivity()).getSlideoutHelper().close();
		switch (position) {
		case 0:
			//profil
			intent = new Intent(getActivity().getApplicationContext(),Home2.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			//this.getActivity().finish();
			break;
		case 1:
			//home
			intent = new Intent(getActivity().getApplicationContext(),Profil.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			//this.getActivity().finish();
			break;
		case 2:
			//countries
			intent = new Intent(getActivity().getApplicationContext(),Countries.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			//this.getActivity().finish();
			break;
		case 3:
			//city
			intent = new Intent(getActivity().getApplicationContext(),City.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			//this.getActivity().finish();
			break;
		case 4:
			//my voucher
			intent = new Intent(getActivity().getApplicationContext(),My_Voucher.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			//this.getActivity().finish();
			break;
		case 5:
			//my channel
			intent = new Intent(getActivity().getApplicationContext(),Channel_My.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			//this.getActivity().finish();
			break;
		case 6:
			//other channel
			intent = new Intent(getActivity().getApplicationContext(),Channel_Lain.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			//this.getActivity().finish();
			break;
		case 7:
			//share 
			//Toast.makeText(this.getActivity().getBaseContext(), "Share, Later", Toast.LENGTH_LONG).show();
			Intent sendIntent = new Intent();
            sendIntent.setAction(Intent.ACTION_SEND);
            sendIntent.putExtra(Intent.EXTRA_TEXT, "Diskonion http://diskonion.com");
            sendIntent.setType("text/plain");
            startActivity(Intent.createChooser(sendIntent, "Share This App"));
			//intent = new Intent(getActivity().getApplicationContext(),My_History.class);
			//intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			//startActivity(intent);
			//this.getActivity().finish();
			break;
		case 8:
			//help
			String urlx = "http://diskonion.com/help";
			intent = new Intent(Intent.ACTION_VIEW, Uri.parse(urlx));
			//activity.startActivity(intent);
			//intent = new Intent(getActivity().getApplicationContext(),Share.class);
			//intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			////this.getActivity().finish();
			break; 
		case 9:
			//about
			String urly = "http://diskonion.com/about";
			intent = new Intent(Intent.ACTION_VIEW, Uri.parse(urly));
			//activity.startActivity(intent);
			//intent = new Intent(getActivity().getApplicationContext(),Share.class);
			//intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			////this.getActivity().finish();
			break; 
		case 10:
			SessionManager session;
	        session = new SessionManager(getActivity().getApplicationContext());
			session.logoutUser();
			intent = new Intent(getActivity().getApplicationContext(),Signin.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			this.getActivity().finish();
			break;
		}
	}
}
